class TouchEvents {
    static sTime;
    static eTime;
    static sPoint = {
        x: undefined,
        y: undefined
    };
    static ePoint = {
        x: undefined,
        y: undefined
    };
    static isClick;
    static srcEle;
    static reload(item, value) {
        if (item) {
            TouchEvents[item] = value;
        } else {
            TouchEvents.sTime = undefined;
            TouchEvents.eTime = undefined;
            TouchEvents.sPoint.x = undefined;
            TouchEvents.sPoint.y = undefined;
            TouchEvents.ePoint.x = undefined;
            TouchEvents.ePoint.y = undefined;
        }
    };
    static mDirection() {
        let xDistance = TouchEvents.ePoint.x - TouchEvents.sPoint.x;
        let yDistance = TouchEvents.ePoint.y - TouchEvents.sPoint.y;
        if (typeof xDistance != "undefined" && typeof yDistance != "undefined") {
            if (Math.abs(xDistance) - Math.abs(yDistance) > 10) {
                return xDistance > 0 ? "right" : "left";
            } else if (Math.abs(yDistance) - Math.abs(xDistance) > 10) {
                return yDistance > 0 ? "down" : "up";
            }
        }
        return undefined;
    }

    static clickOpType(ele, callback) {
        ele.addEventListener("touchstart", function(e) {
            TouchEvents.sTime = new Date();
            TouchEvents.srcEle = e.target;
            TouchEvents.isClick = true;
            TouchEvents.sPoint.x = e.touches[0].clientX;
            TouchEvents.sPoint.y = e.touches[0].clientY;
        });
        ele.addEventListener("touchmove", function(e) {
            TouchEvents.ePoint.x = e.touches[0].clientX;
            TouchEvents.ePoint.y = e.touches[0].clientY;
            if (TouchEvents.mDirection()) TouchEvents.isClick = false;
        });
        ele.addEventListener("touchend", function() {
            TouchEvents.eTime = new Date();
            let res = null;
            if (TouchEvents.isClick) {
                if (TouchEvents.eTime - TouchEvents.sTime < 200) {
                    res = {
                        type: "click",
                        direction: "click",
                        srcEle: TouchEvents.srcEle
                    };
                } else if (TouchEvents.eTime - TouchEvents.sTime >= 500) {
                    res = {
                        type: "click",
                        direction: "press",
                        srcEle: TouchEvents.srcEle
                    };
                }
            } else {
                res = {
                    type: "move",
                    direction: TouchEvents.mDirection(),
                    srcEle: TouchEvents.srcEle
                };
            }
            if (res && typeof callback == "function") {
                callback(res);
            } else {
                console.log(res ? res : "不在有效操作阈值区间内,无效的操作.");
            }
        });
    }
}
/*  --- 外部调用 ---
    window.onload = function() {
        TouchEvents.clickOpType(document.querySelector("body"));
    }
*/
export default TouchEvents.clickOpType;